import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
//import './api/mock'
import Cookie from 'js-cookie'
Vue.config.productionTip = false
Vue.use(ElementUI);

import axios from 'axios'
import VueAxios from 'vue-axios'

axios.defaults.withCredentials = true
Vue.use(VueAxios, axios);

axios.defaults.baseURL = 'http://101.43.44.19'

axios.interceptors.request.use(config => {
  config.headers.token = Cookie.get("token")
  return config
})

router.beforeEach((to, from, next) => {
  const token = Cookie.get('token')
  if (!token && to.path === '/register' || !token && to.path === '/deliverHome' || !token && to.path === '/forgetPassword') {
    next();
  } else {
    if (!token && (to.path !== '/')) {
      next('/');
    } else if (token && to.path === '/') {
      next('/adminHome')
    } else {
      next();
    }
  }

})

new Vue({
  router,
  store,
  render: h => h(App),
  created() {
    store.commit('addMenu', router)
  }
}).$mount('#app')
